//
//  ViewController.m
//  IOSLearn
//
//  Created by Yanyan Jiang on 2019/2/24.
//  Copyright © 2019 Yanyan Jiang. All rights reserved.
//

#import "ViewController.h"
#import "NotificationAC.h"
#import "NotificationBC.h"
#import "YYPracticeController.h"
#import "YYTestObjectModel.h"

#import <Foundation/NSKeyValueObserving.h>

@interface ViewController ()
@property(nonatomic, strong) NotificationAC *aCC;
@property(nonatomic, strong) YYTestObjectModel *objectModel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"aaaa";
    [[NSNotificationCenter defaultCenter] postNotificationName:@"postCenter" object:self userInfo:@{@"keyName":@"userInfo"}];
    YYTestObjectModel *testModel = [[YYTestObjectModel alloc] init];
    [testModel setValue:@"yanyanjiang" forKey:@"name"];
    NSString *nameStr = [testModel valueForKey:@"name"];
    NSString *ageStr = [testModel valueForKey:@"age"];
    NSLog(@"nameStr:%@",nameStr);
    NSLog(@"ageStr:%@",ageStr);
    
    YYTest1ObjectModel *test1 = [[YYTest1ObjectModel alloc] init];
    [testModel setValue:test1 forKey:@"test1"];
    [testModel setValue:@"yanfangjiang" forKeyPath:@"test1.name"];
    NSLog(@"test1NameStr:%@",[testModel valueForKeyPath:@"test1.name"]);
    
    [testModel setValue:nil forKey:@"height"];
    NSLog(@"testModel的身高是：%@",[testModel valueForKey:@"height"]);
    
    [testModel setValue:@"200" forKey:@"weight"];
    NSLog(@"testModel的体重是：%@",[testModel valueForKey:@"wheight"]);
    
    NSNumber *handLong = @11;
    NSError *error;
    NSString *key = @"handLong";
    BOOL isValidate =  [testModel validateValue:&handLong forKeyPath:key error:&error];
    if (isValidate) {
        NSLog(@"键值匹配");
        [testModel setValue:handLong forKey:key];
    }else{
        NSLog(@"键值不匹配");
    }
    NSLog(@"testModel的手长是：%@",[testModel valueForKey:key]);
    
    _objectModel = [[YYTestObjectModel alloc] init];
    [_objectModel addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];
    [self initSubviews];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"name"]) {
        NSLog(@"name:%@",[object valueForKey:@"name"]);
        NSLog(@"changeInfo:%@",change);
    }
}

- (void)dealloc{
    [_objectModel removeObserver:self forKeyPath:@"name"];
}


- (void)initSubviews{
    NSInteger ww =  [UIScreen mainScreen].bounds.size.width;
    NSInteger hh =  [UIScreen mainScreen].bounds.size.height;

    UIScrollView  *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,ww,hh)];
    sv.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    sv.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width,700);
    sv.showsHorizontalScrollIndicator = NO;
    sv.showsVerticalScrollIndicator = NO;
    sv.backgroundColor = [UIColor whiteColor];
    sv.pagingEnabled = NO;
    sv.bounces = NO;
    [self.view addSubview:sv];

    NSArray *titleArr = @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H"];
    NSInteger xInter = (ww-5*50)/6;
    for (int i=0; i<titleArr.count; i++) {
        UIButton *timeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        timeBtn.frame = CGRectMake((i%5)*(50+xInter)+xInter, 20+(i/5)*(20+50), 50, 50);
        [timeBtn setTitle:titleArr[i] forState:UIControlStateNormal];
        timeBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        timeBtn.backgroundColor = [UIColor greenColor];
        [timeBtn setTitleColor:[UIColor magentaColor] forState:UIControlStateNormal];
        [timeBtn addTarget:self action:@selector(clickTimeBtns:) forControlEvents:UIControlEventTouchUpInside];
        timeBtn.tag = 1+i;
        [sv addSubview:timeBtn];
    }
}


- (void)clickTimeBtns:(UIButton*)btn{
    switch (btn.tag) {
        case 1:{
            _aCC = [[NotificationAC alloc] init];
            _aCC.view.backgroundColor = [UIColor whiteColor];
            [self.navigationController pushViewController:_aCC animated:YES];
            _objectModel.name = @"张三";
            _objectModel.name = @"李四";
        }
            break;
        case 2:{
            NotificationBC *bCC = [[NotificationBC alloc] init];
            bCC.view.backgroundColor = [UIColor whiteColor];
            [self.navigationController pushViewController:bCC animated:YES];
        }
            break;
        case 3:{
            YYPracticeController *cCC = [[YYPracticeController alloc] init];
            cCC.view.backgroundColor = [UIColor whiteColor];
            [self.navigationController pushViewController:cCC animated:YES];
        }
            break;
        case 4:{
            
        }
            break;
        case 5:{
            
        }
            break;
        case 6:{
            
        }
            break;
        case 7:{
            
        }
            break;

        case 8:{
            
        }
            break;

        case 9:{
            
        }
            break;

        default:
            break;
    }
}

@end
